import java.util.Arrays;

public class practise {
    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5,6,7,8,9};
        //System.out.println(binarySearch(arr,6));
        String newArr = Arrays.toString(foundArr());
        System.out.println(newArr);
        //String newArr = toString(arr);
        //System.out.println(newArr);
    }
    //数组转字符串
    public static String toString(int[] arr){
        String ret = "[";
        for (int i = 0; i < arr.length; i++) {
            ret +=arr[i];
            if(i !=arr.length -1){
                ret +=",";
            }
        }
        ret += "]";
        return ret;
    }
    //找最大元素
    public static int max(int[] arr){
        int max = arr[0];
        for (int i = 0; i < arr.length; i++) {
            if(arr[i] > max){
                max = arr[i];
            }
        }
        return max;
    }
    //求和
    public static int sum(int[] arr){
        int sum = 0;
        for (int i = 0; i < arr.length; i++) {
            sum += arr[i];
        }
        return sum;
    }
    //求平均值
    public static double avg(int[] arr){
        int sum = 0;
        for(int x : arr){
            sum += x;
        }
//        for (int i = 0; i < arr.length; i++) {
//            sum += arr[i];
//        }
        return (double)sum / (double)arr.length;
    }
    //拷贝数组
    public static int[] copyOf(int[] arr){
        int[] ret = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            ret[i] = arr[i];
        }
        return ret;
    }
    //改变元素的值
    public static int[] transForm(int[] arr){
        int[] ret = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            ret[i] = arr[i] * 2;
        }
        return ret;
    }
    //打印元素的值
    public static void print(int[] arr){
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]+" ");
        }
    }
    //创建数组并赋值
    public static int[] foundArr(){
        int[] arr = new int[100];
        for (int i = 0; i < 100; i++) {
            arr[i] = i + 1;
        }
        return arr;
    }
    //判断是否有序
    public static boolean plain(int[] arr){
        boolean result = true;
        for (int i = 0; i < arr.length-1; i++) {
            if(arr[i] > arr[i+1]){
                result = false;
            }
        }
        return result;
    }
    //顺序查找
    public static int find(int[] arr,int toFind){
        for (int i = 0; i < arr.length; i++) {
            if(arr[i] == toFind){
                return i;
            }
        }
        return -1;
    }
    //二分查找
    public static int binarySearch(int[] arr,int toFind){
        int left = 0;
        int right = arr.length - 1;
        while (left <= right){
            int mid = (left + right) >> 1;
            //int mid = (left + right) / 2;
            if(toFind < arr[mid]){
                right = mid - 1;
            }else if(toFind > arr[mid]){
                left = mid + 1;
            }else {
                return mid;
            }
        }
        return -1;
    }
    //递归二分查找
    public static int recursionBinarySearch(int[] arr,int left,int right,int toFind){
        int mid = (left + right) / 2;
        if(arr[mid] == toFind){
            return mid;
        }
        if(left >= right){
            return -1;
        }else if(toFind < arr[mid]){
            recursionBinarySearch(arr,left,mid-1,toFind);
        }else {
            recursionBinarySearch(arr,mid+1,right,toFind);
        }
        return -1;
    }
    //冒泡排序
    public static void bubbleSort(int[] arr){
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if(arr[j] > arr[j+1]){
                    int tmp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = tmp;
                }
            }
        }
    }
    //逆序排列
    public static void reverse(int[] arr){
        int left = 0;
        int right = arr.length - 1;
        while (left < right){
            int tmp = arr[left];
            arr[left] = arr[right];
            arr[right] = tmp;
            left++;
            right--;
        }
    }
    public static void transform(int[] arr){
        int left = 0;
        int right = arr.length - 1;
        while(left < right){
            while (left < right && arr[left] % 2 == 0){
                //找奇数
                left++;
            }
            while ((left < right && arr[right] % 2 != 0)){
                right--;
            }
            int tmp = arr[left];
            arr[left] = arr[right];
            arr[right] = tmp;
        }
    }
    //连续奇数
    public static boolean judgeOddNumber(int[] arr){
        boolean result = false;
        for (int i = 0; i < arr.length-2; i++) {
            if(arr[i] % 2 !=0 && arr[i+1] % 2 != 0 && arr[i+2] % 2 != 0){
                result = true;
            }
        }
        return result;
    }
   //    2/n
    public static boolean CheckInvalidArray(int[] arr){
        //检查是否为无效数据
        boolean g_bInputInvalid = false;
        g_bInputInvalid = false;
        if(arr == null || arr.length <= 0) {
            g_bInputInvalid = true;
        }
        return g_bInputInvalid;

    }
//    public static boolean CheckMoreThanHalf(int[] arr,int number){
//    //检查出现次数最多的那个数字是否大于1/2数组长度
//        boolean g_bInputInvalid = false;
//        int times = 0;
//        for(int i=0;i<arr.length;++i) {
//            if(arr[i] == number)
//                times++;
//        }
//        boolean isMoreThanHalf = true;
//        if(times * 2 <= arr.length) {
//            g_bInputInvalid =true;
//            isMoreThanHalf = false;
//        }
//        return isMoreThanHalf;
//    }
//
//    public static int majorityElement(int[] arr) {
//        //找出现次数最多的
//        if(CheckInvalidArray(arr)) {
//            return -1;
//        }
//        int result = arr[0];
//        int times = 1;
//        for(int i=1;i<arr.length;++i) {
//            if(times == 0) {
//                result = arr[i];
//                times = 1;
//            }
//            else if(arr[i] == result) {
//                times++;
//            }
//            else {
//                times--;
//            }
//        }
//        if(!CheckMoreThanHalf(arr,result)) {
//            result = 0;
//        }
//        return result;
//    }
//    //只出现一次的数字
//    public static int findNumsAppearOnce(int[] arr){
//        int num = 0;
//        for (int i = 0; i < arr.length; i++) {
//            num = num^arr[i];
//        }
//        return num;
//    }
    //两数之和
    public static int[] twoSum(int[] arr,int target){
        int[] arr2 = new int[2];
        for (int i = 0; i < arr.length; i++) {
            for (int j = i+1; j < arr.length; j++) {
                if(arr[i] + arr[j] == target){
                    arr2[0] = i;
                    arr2[1] = j;
                }
            }
        }
        return arr2;
    }
}
